﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using DTO;
using UTIL;

namespace DAO
{
    public class SanPhamThietKeDao
    {
        public static bool ThemSanPhamThietKe(SanPhamThietKeDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_themSanPhamThietKe");
                StoreUtilities.AddInputPara(cmd, "@IDhinhmau", SqlDbType.Int, one.IDHinhMau);
                StoreUtilities.AddInputPara(cmd, "@IDnguoidung", SqlDbType.Int, one.IDNguoiDung);
                StoreUtilities.AddInputPara(cmd, "@thoidiem", SqlDbType.DateTime, one.ThoiDiem);
                StoreUtilities.AddInputPara(cmd, "@IDhinhtrangtri", SqlDbType.Int, one.IDHinhTrangTri);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
                throw ex;

            }
            return result;
        }

        public static bool XoaSanPhamThietKe(SanPhamThietKeDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_xoaSanPhamThietKe");
                StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, one.ID);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
            }
            return result;
        }

        public static bool CapnhatSanPhamThietKe(SanPhamThietKeDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_capnhatSanPhamThietKe");
                StoreUtilities.AddInputPara(cmd, "@IDhinhmau", SqlDbType.Int, one.IDHinhMau);
                StoreUtilities.AddInputPara(cmd, "@thoidiem", SqlDbType.DateTime, one.ThoiDiem);
                StoreUtilities.AddInputPara(cmd, "@IDhinhtrangtri", SqlDbType.Int, one.IDHinhTrangTri);
                StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, one.ID);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
                throw ex;

            }
            return result;
        }

        public static List<SanPhamThietKeDto> LayDanhSachSanPhamThietKeCuaNguoiDung(int IDnguoidung)
        {
            // tao ket noi CSDL 
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_layDanhSachSanPhamThietKe");
            StoreUtilities.AddInputPara(cmd, "@IDnguoidung", SqlDbType.Int, IDnguoidung);

            SqlDataReader dr = cmd.ExecuteReader();

            List<SanPhamThietKeDto> danhsach = new List<SanPhamThietKeDto>();
            SanPhamThietKeDto sanpham = null;
            while (dr.Read())
            {
                sanpham = new SanPhamThietKeDto();
                sanpham.ID = (int)dr["ID"];
                sanpham.IDHinhTrangTri = (int)dr["IDHinhTrangTri"];
                sanpham.IDHinhMau = (int)dr["IDHinhMau"];
                sanpham.IDNguoiDung = (int)dr["IDNguoiDung"];
                sanpham.ThoiDiem = (DateTime)dr["ThoiDiem"];

                danhsach.Add(sanpham);
            }
            // dong ket noi
            cnn.Close();

            return danhsach;
        }

        public static SanPhamThietKeDto LaySanPhamThietKeTheoID(int ID)
        {
            // ket noi CSDL
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_laySanPhamThietKeTheoID");
            StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, ID);

            SqlDataReader dr = cmd.ExecuteReader();
            SanPhamThietKeDto sanpham = null;
            while (dr.Read())
            {
                sanpham = new SanPhamThietKeDto();
                sanpham.ID = (int)dr["ID"];
                sanpham.IDHinhTrangTri = (int)dr["IDHinhTrangTri"];
                sanpham.IDHinhMau = (int)dr["IDHinhMau"];
                sanpham.IDNguoiDung = (int)dr["IDNguoiDung"];
                sanpham.ThoiDiem = (DateTime)dr["ThoiDiem"];
            }
            // dong ket noi
            cnn.Close();

            return sanpham;
        }

        public static bool kiemtraTonTai(SanPhamThietKeDto one)
        {
            bool result = false;
            SanPhamThietKeDto temp = LaySanPhamThietKeTheoID(one.ID);
            if (temp != null)
                result = true;

            return result;
        }
    }
}
